package cn.iocoder.yudao.module.blog.convert.jiucase;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.controller.admin.jiutag.vo.BlogJiuTagRespVO;
import cn.iocoder.yudao.module.blog.controller.app.jiucase.vo.AppBlogJiuCaseRespVO;
import cn.iocoder.yudao.module.blog.convert.base.BlogConvert;
import cn.iocoder.yudao.module.blog.convert.jiutag.BlogJiuTagConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiutag.BlogJiuTagDO;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import jodd.util.StringUtil;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import cn.iocoder.yudao.module.blog.controller.admin.jiucase.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiucase.BlogJiuCaseDO;

/**
 * 灸法案例 Convert
 *
 * @author 曹建伟
 */
@Mapper
public interface BlogJiuCaseConvert {

    BlogJiuCaseConvert INSTANCE = Mappers.getMapper(BlogJiuCaseConvert.class);

    BlogJiuCaseDO convert(BlogJiuCaseCreateReqVO bean);

    BlogJiuCaseDO convert(BlogJiuCaseUpdateReqVO bean);

    BlogJiuCaseRespVO convert(BlogJiuCaseDO bean);

    List<BlogJiuCaseRespVO> convertList(List<BlogJiuCaseDO> list);

    PageResult<BlogJiuCaseRespVO> convertPage(PageResult<BlogJiuCaseDO> page);

    List<BlogJiuCaseExcelVO> convertList02(List<BlogJiuCaseDO> list);


    default AppBlogJiuCaseRespVO convertApp(BlogJiuCaseDO bean, MemberUserRespDTO memberUserRespDTO, Map<Long, BlogJiuTagDO> tagMap){
        if ( bean == null ) {
            return null;
        }

        AppBlogJiuCaseRespVO appBlogJiuCaseRespVO = new AppBlogJiuCaseRespVO();
        appBlogJiuCaseRespVO.setMember(BlogConvert.INSTANCE.covnertMember(memberUserRespDTO));
        appBlogJiuCaseRespVO.setMemberId(bean.getMemberId());
        appBlogJiuCaseRespVO.setId(bean.getId());
        appBlogJiuCaseRespVO.setTags(bean.getTags());
        appBlogJiuCaseRespVO.setName(bean.getName());
        appBlogJiuCaseRespVO.setSex(bean.getSex());
        appBlogJiuCaseRespVO.setAge(bean.getAge());
        appBlogJiuCaseRespVO.setPhone(bean.getPhone());
        appBlogJiuCaseRespVO.setWeight(bean.getWeight());
        appBlogJiuCaseRespVO.setLevelId(bean.getLevelId());

        appBlogJiuCaseRespVO.setTitle(bean.getTitle());
        appBlogJiuCaseRespVO.setPicUrl(bean.getPicUrl());
        appBlogJiuCaseRespVO.setVideoUrl(bean.getVideoUrl());
        appBlogJiuCaseRespVO.setAudioUrl(bean.getAudioUrl());
        appBlogJiuCaseRespVO.setContent(bean.getContent());
        appBlogJiuCaseRespVO.setCreateTime(bean.getCreateTime());

        appBlogJiuCaseRespVO.setReadNum(bean.getReadNum());
        appBlogJiuCaseRespVO.setCommentNum(bean.getCommentNum());
        appBlogJiuCaseRespVO.setLikeNum(bean.getLikeNum());

        appBlogJiuCaseRespVO.setShowType(bean.getShowType());

        List<BlogJiuTagRespVO> tagList = new ArrayList<>();
        if(bean.getTags() != null && StringUtil.isNotEmpty(bean.getTags())){
            String[] tagStrIds = bean.getTags().split(";");
            for(int i = 0; i<tagStrIds.length; i++){
                tagList.add(BlogJiuTagConvert.INSTANCE.convert(tagMap.get(Long.parseLong(tagStrIds[i]))));
            }
        }

        appBlogJiuCaseRespVO.setTagList(tagList);

        return appBlogJiuCaseRespVO;
    }

    default List<AppBlogJiuCaseRespVO> convertAppList(List<BlogJiuCaseDO> blogJiuCaseDOList, Map<Long, MemberUserRespDTO> memberMap, Map<Long, BlogJiuTagDO> tagMap){
        List<AppBlogJiuCaseRespVO> list = new ArrayList<>();
        blogJiuCaseDOList.forEach(jiuCaseDO -> {
            list.add(convertApp(jiuCaseDO, memberMap.get(jiuCaseDO.getMemberId()), tagMap));
        });

        return list;
    }

    default PageResult<AppBlogJiuCaseRespVO> convertAppPage(PageResult<BlogJiuCaseDO> page, Map<Long, MemberUserRespDTO> memberMap, Map<Long, BlogJiuTagDO> tagMap){
        if ( page == null ) {
            return null;
        }

        PageResult<AppBlogJiuCaseRespVO> pageResult = new PageResult<AppBlogJiuCaseRespVO>();
        List<AppBlogJiuCaseRespVO> list = convertAppList(page.getList(), memberMap, tagMap);

        pageResult.setList(list);
        pageResult.setTotal(page.getTotal());

        return pageResult;
    }

}
